package cn.wen.java.utils;

import java.lang.reflect.Array;
import java.util.Collection;
import java.util.Enumeration;
import java.util.Iterator;
import java.util.Map;

import cn.wen.java.lang.HelperOfObject;

/**
 * Title：集合辅助类
 * <p>
 * Description：
 * <p>
 * Copyright：Copyright (c) 2010-2012
 * 
 * @author ctwen, 温承瑭
 * @email ct_wen@qq.com
 * @version Revision：1.0 Date：2011/1/6 Description：初完成。
 *          <p>
 */
public class HelperOfCollection {

    /**
     * Return <code>true</code> if the supplied Collection is <code>null</code> or empty. Otherwise, return <code>false</code>.
     * 
     * @param collection the Collection to check
     * @return whether the given Collection is empty
     */
    public static boolean isEmpty(Collection<?> collection) {
	return (collection == null || collection.isEmpty());
    }

    /**
     * Return <code>true</code> if the supplied Map is <code>null</code> or empty. Otherwise, return <code>false</code>.
     * 
     * @param map the Map to check
     * @return whether the given Map is empty
     */
    public static boolean isEmpty(Map<?, ?> map) {
	return (map == null || map.isEmpty());
    }

    /**
     * Check whether the given Iterator contains the given element.
     * 
     * @param iterator the Iterator to check
     * @param element the element to look for
     * @return <code>true</code> if found, <code>false</code> else
     */
    public static boolean contains(Iterator<?> iterator, Object element) {
	if (iterator != null) {
	    while (iterator.hasNext()) {
		Object candidate = iterator.next();
		if (HelperOfObject.equals(candidate, element)) {
		    return true;
		}
	    }
	}
	return false;
    }

    /**
     * Check whether the given Enumeration contains the given element.
     * 
     * @param enumeration the Enumeration to check
     * @param element the element to look for
     * @return <code>true</code> if found, <code>false</code> else
     */
    public static boolean contains(Enumeration<?> enumeration, Object element) {
	if (enumeration != null) {
	    while (enumeration.hasMoreElements()) {
		Object candidate = enumeration.nextElement();
		if (HelperOfObject.equals(candidate, element)) {
		    return true;
		}
	    }
	}
	return false;
    }

    /**
     * Gets the size of the collection/iterator specified.
     * <p>
     * This method can handles objects as follows
     * <ul>
     * <li>Collection - the collection size
     * <li>Map - the map size
     * <li>Array - the array size
     * <li>Iterator - the number of elements remaining in the iterator
     * <li>Enumeration - the number of elements remaining in the enumeration
     * </ul>
     * 
     * @param object the object to get the size of
     * @return the size of the specified collection
     * @throws IllegalArgumentException thrown if object is not recognised or null
     * @since Commons Collections 3.1
     */
    public static int size(Object object) {
	int total = 0;
	if (object instanceof Map) {
	    total = ((Map<?, ?>) object).size();
	}
	else if (object instanceof Collection) {
	    total = ((Collection<?>) object).size();
	}
	else if (object instanceof Object[]) {
	    total = ((Object[]) object).length;
	}
	else if (object instanceof Iterator) {
	    Iterator<?> it = (Iterator<?>) object;
	    while (it.hasNext()) {
		total++;
		it.next();
	    }
	}
	else if (object instanceof Enumeration) {
	    Enumeration<?> it = (Enumeration<?>) object;
	    while (it.hasMoreElements()) {
		total++;
		it.nextElement();
	    }
	}
	else if (object == null) {
	    throw new IllegalArgumentException("Unsupported object type: null");
	}
	else {
	    try {
		total = Array.getLength(object);
	    }
	    catch (IllegalArgumentException ex) {
		throw new IllegalArgumentException("Unsupported object type: " + object.getClass().getName());
	    }
	}
	return total;
    }

}
